package com.jspgou.cms.action.store;

import static com.jspgou.cms.Constants.STORE_SYS;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jspgou.cms.entity.ProductType;
import com.jspgou.cms.entity.ProductTypeProperty;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.manager.ProductTypeMng;
import com.jspgou.cms.manager.ProductTypePropertyMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;
import com.jspgou.core.web.front.URLHelper;

@Controller
public class TypeFroAct {
	private static final Logger log = LoggerFactory.getLogger(TypeFroAct.class);
    /**
     * 店铺类别列表页
    */
    public static final String TPL_TYPE_STORE_LIST = "tpl.typeStoreList";
    /**
     * 店铺属性列表页
    */
    public static final String TPL_PROPERTY_STORE_LIST = "tpl.propertyStoreList";
    /**
     * 店铺属性添加页
    */
    public static final String TPL_PROPERTY_STORE_ADD = "tpl.propertyStoreAdd";
    /**
     * 店铺属性修改页
    */
    public static final String TPL_PROPERTY_STORE_EDIT = "tpl.propertyStoreEdit";
	
    //商品类别列表
	@RequestMapping(value = "/type/list*.jspx")
	public String list(String cl,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		List<ProductType> list = productTypeMng.getList(web.getId());
		model.addAttribute("list", list);
		if(member==null){
			return "redirect:../login.jspx";
		}
		int pageNo = URLHelper.getPageNo(request);
		ShopFrontHelper.setCommonData(request, model, web);
		model.addAttribute("cl", cl);
		model.addAttribute("pageNo", pageNo);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_TYPE_STORE_LIST), request);
	}
	
    //商品属性列表
	@RequestMapping(value = "/type/propertyList*.jspx")
	public String propertyList(Long typeId,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		Store store=member.getStore();
		if(member==null){
			return "redirect:../login.jspx";
		}
		ProductType pType = productTypeMng.findById(typeId);
		List<ProductTypeProperty> list=typePropertyMng.getList(typeId,store.getId(), false);
		model.addAttribute("list", list);
		model.addAttribute("typeId", typeId);
		model.addAttribute("fieldList", getFieldList(list));
		model.addAttribute("pType", pType);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_PROPERTY_STORE_LIST), request);
	}
	
    //商品属性添加页
	@RequestMapping(value = "/type/propertyAdd.jspx")
	public String propertyAdd(Long typeId,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		Store store=member.getStore();
		if(member==null){
			return "redirect:../login.jspx";
		}
		ProductTypeProperty property = typePropertyMng.findById(typeId);
		model.addAttribute("property", property);
		model.addAttribute("typeId", typeId);
		model.addAttribute("store", store);
		int pageNo = URLHelper.getPageNo(request);
		ShopFrontHelper.setCommonData(request, model, web);
		model.addAttribute("pageNo", pageNo);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_PROPERTY_STORE_ADD), request);
	}
	
	 //商品属性修改页
	@RequestMapping("/type/propertyEdit.jspx")
	public String propertyEdit(Long id,Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		Store store=member.getStore();
		if(member==null){
			return "redirect:../login.jspx";
		}
		ProductTypeProperty property = typePropertyMng.findById(id);
		model.addAttribute("store", store);
		model.addAttribute("property", property);
		model.addAttribute("typeId", property.getPropertyType().getId());
		model.addAttribute("isCategory", property.getCategory());
		ShopFrontHelper.setCommonData(request, model, web);
		model.addAttribute("pageNo", pageNo);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_PROPERTY_STORE_EDIT), request);
	}
	
	@RequestMapping(value = "/type/propertySave.jspx")
	public String propertySave(ProductTypeProperty bean,Long typeId,
			HttpServletRequest request,ModelMap model){
		ShopMember member=CmsUtils.getMember(request);
		Store store=member.getStore();
		if(member==null){
			return "redirect:../login.jspx";
		}
		ProductType type = new ProductType();
		type.setId(typeId);
		bean.setPropertyType(type);
		bean.setSingle(true);
		bean.setCustom(true);
		bean.setStore(store);
		typePropertyMng.save(bean);
		model.addAttribute("typeId", typeId);
		model.addAttribute("isCategory", bean.getCategory());
		return propertyList(typeId,request,model);
	}
	
	@RequestMapping(value = "/type/propertyUpdate.jspx")
	public String propertyUpdate(ProductTypeProperty bean,Long typeId,boolean category,
			HttpServletRequest request,ModelMap model){
		typePropertyMng.update(bean);
		model.addAttribute("typeId", typeId);
		model.addAttribute("isCategory", category);
		return propertyList(typeId,request,model);
	}
	
	@RequestMapping("/type/propertyDelete.jspx")
	public String propertyDelete(Long[] ids, Long typeId, Boolean isCategory,
			HttpServletRequest request, ModelMap model) {
		ProductTypeProperty[] beans = typePropertyMng.deleteByIds(ids);
		for (ProductTypeProperty bean : beans) {
			log.info("delete CmsModelItem id={}", bean.getId());
		}
		model.addAttribute("typeId", typeId);
		model.addAttribute("isCategory", isCategory);
		return "redirect:propertyList.jspx";
	}
	
	private List<String> getFieldList(List<ProductTypeProperty> items) {
		List<String> list = new ArrayList<String>(items.size());
		for (ProductTypeProperty item : items) {
			list.add(item.getField());
		}
		return list;
	}
	
	@Autowired
	private ProductTypeMng productTypeMng;
	@Autowired
	private ProductTypePropertyMng typePropertyMng;

}
